package com.ruoyi.project.system.controller;

import java.util.*;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.OperationRecord;
import com.ruoyi.project.system.service.IOperationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.InstitutionalManagement;
import com.ruoyi.project.system.service.IInstitutionalManagementService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 五项制度Controller
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/system/management")
public class InstitutionalManagementController extends BaseController
{
    @Autowired
    private IInstitutionalManagementService institutionalManagementService;

    @Autowired
    private IOperationRecordService operationRecordService;

    /**
     * 查询五项制度列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InstitutionalManagement institutionalManagement)
    {
        startPage();
        List<InstitutionalManagement> list = institutionalManagementService.selectInstitutionalManagementList(institutionalManagement);
        for(InstitutionalManagement management : list){
            management.setRoleKey(SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey());
        }
        return getDataTable(list);
    }

    /**
     * 导出五项制度列表
     */
    @Log(title = "五项制度", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InstitutionalManagement institutionalManagement)
    {
        List<InstitutionalManagement> list = institutionalManagementService.selectInstitutionalManagementList(institutionalManagement);
        ExcelUtil<InstitutionalManagement> util = new ExcelUtil<InstitutionalManagement>(InstitutionalManagement.class);
        return util.exportExcel(list, "五项制度数据");
    }

    /**
     * 获取五项制度详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        InstitutionalManagement institutionalManagement = institutionalManagementService.selectInstitutionalManagementById(id);
        List<Map<String,Object>> fileUrlList = new ArrayList<>();
        for(int i =0;i<Arrays.asList(institutionalManagement.getFileUrl().split(",")).size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("url",Arrays.asList(institutionalManagement.getFileUrl().split(",")).get(i));
            map.put("name",Arrays.asList(institutionalManagement.getFileName().split(",")).get(i));
            map.put("url2",Arrays.asList(institutionalManagement.getFileTwoUrl().split(",")).get(i));
            map.put("name2",Arrays.asList(institutionalManagement.getFileTwoName().split(",")).get(i));
            map.put("url3",Arrays.asList(institutionalManagement.getFileThreeUrl().split(",")).get(i));
            map.put("name3",Arrays.asList(institutionalManagement.getFileThreeName().split(",")).get(i));
            map.put("url4",Arrays.asList(institutionalManagement.getFileFourUrl().split(",")).get(i));
            map.put("name4",Arrays.asList(institutionalManagement.getFileFourName().split(",")).get(i));
            map.put("url5",Arrays.asList(institutionalManagement.getFileFourUrl().split(",")).get(i));
            map.put("name5",Arrays.asList(institutionalManagement.getFileFiveName().split(",")).get(i));
            fileUrlList.add(map);
        }
        institutionalManagement.setFileUrlList(fileUrlList);
        return AjaxResult.success(institutionalManagement);
    }

    /**
     * 新增五项制度
     */
    @Log(title = "五项制度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InstitutionalManagement institutionalManagement)
    {
        institutionalManagement.setFileUrl(institutionalManagement.getFileUrl()==null?"":institutionalManagement.getFileUrl().substring(0,institutionalManagement.getFileUrl().length()-1));
        institutionalManagement.setFileName(institutionalManagement.getFileName()==null?"":institutionalManagement.getFileName().substring(0,institutionalManagement.getFileName().length()-1));
        institutionalManagement.setFileTwoUrl(institutionalManagement.getFileTwoUrl()==null?"":institutionalManagement.getFileTwoUrl().substring(0,institutionalManagement.getFileTwoUrl().length()-1));
        institutionalManagement.setFileTwoName(institutionalManagement.getFileTwoName()==null?"":institutionalManagement.getFileTwoName().substring(0,institutionalManagement.getFileTwoName().length()-1));
        institutionalManagement.setFileThreeName(institutionalManagement.getFileThreeName()==null?"":institutionalManagement.getFileThreeName().substring(0,institutionalManagement.getFileThreeUrl().length()-1));
        institutionalManagement.setFileThreeUrl(institutionalManagement.getFileThreeUrl()==null?"":institutionalManagement.getFileThreeUrl().substring(0,institutionalManagement.getFileThreeUrl().length()-1));
        institutionalManagement.setFileFourUrl(institutionalManagement.getFileFourUrl()==null?"":institutionalManagement.getFileFourUrl().substring(0,institutionalManagement.getFileFourUrl().length()-1));
        institutionalManagement.setFileFourName(institutionalManagement.getFileFourName()==null?"":institutionalManagement.getFileFourName().substring(0,institutionalManagement.getFileFourName().length()-1));
        institutionalManagement.setFileFiveUrl(institutionalManagement.getFileFiveUrl()==null?"":institutionalManagement.getFileFiveUrl().substring(0,institutionalManagement.getFileFiveUrl().length()-1));
        institutionalManagement.setFileFiveName(institutionalManagement.getFileFiveName()==null?"":institutionalManagement.getFileFiveName().substring(0,institutionalManagement.getFileFiveName().length()-1));
        institutionalManagement.setStatus("待审核");
        int i = institutionalManagementService.insertInstitutionalManagement(institutionalManagement);
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setContent("上传了附件");
        operationRecord.setManageId(institutionalManagement.getId());
        operationRecord.setTime(new Date());
        operationRecord.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        operationRecordService.insertOperationRecord(operationRecord);
        return toAjax(i);
    }

    /**
     * 修改五项制度
     */
    @Log(title = "五项制度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InstitutionalManagement institutionalManagement)
    {
        institutionalManagement.setFileUrl(institutionalManagement.getFileUrl()==null?"":institutionalManagement.getFileUrl().substring(0,institutionalManagement.getFileUrl().length()-1));
        institutionalManagement.setFileName(institutionalManagement.getFileName()==null?"":institutionalManagement.getFileName().substring(0,institutionalManagement.getFileName().length()-1));
        institutionalManagement.setFileTwoUrl(institutionalManagement.getFileTwoUrl()==null?"":institutionalManagement.getFileTwoUrl().substring(0,institutionalManagement.getFileTwoUrl().length()-1));
        institutionalManagement.setFileTwoName(institutionalManagement.getFileTwoName()==null?"":institutionalManagement.getFileTwoName().substring(0,institutionalManagement.getFileTwoName().length()-1));
        institutionalManagement.setFileThreeName(institutionalManagement.getFileThreeName()==null?"":institutionalManagement.getFileThreeName().substring(0,institutionalManagement.getFileThreeUrl().length()-1));
        institutionalManagement.setFileThreeUrl(institutionalManagement.getFileThreeUrl()==null?"":institutionalManagement.getFileThreeUrl().substring(0,institutionalManagement.getFileThreeUrl().length()-1));
        institutionalManagement.setFileFourUrl(institutionalManagement.getFileFourUrl()==null?"":institutionalManagement.getFileFourUrl().substring(0,institutionalManagement.getFileFourUrl().length()-1));
        institutionalManagement.setFileFourName(institutionalManagement.getFileFourName()==null?"":institutionalManagement.getFileFourName().substring(0,institutionalManagement.getFileFourName().length()-1));
        institutionalManagement.setFileFiveUrl(institutionalManagement.getFileFiveUrl()==null?"":institutionalManagement.getFileFiveUrl().substring(0,institutionalManagement.getFileFiveUrl().length()-1));
        institutionalManagement.setFileFiveName(institutionalManagement.getFileFiveName()==null?"":institutionalManagement.getFileFiveName().substring(0,institutionalManagement.getFileFiveName().length()-1));
        return toAjax(institutionalManagementService.updateInstitutionalManagement(institutionalManagement));
    }

    /**
     * 删除五项制度
     */
    @Log(title = "五项制度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(institutionalManagementService.deleteInstitutionalManagementByIds(ids));
    }

    @PostMapping("/submitAgain")
    public AjaxResult submitAgain(@RequestBody InstitutionalManagement institutionalManagement)
    {
        institutionalManagement.setStatus("待审核");
        institutionalManagement.setRemark("");
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setContent("再次提交");
        operationRecord.setManageId(institutionalManagement.getId());
        operationRecord.setTime(new Date());
        operationRecord.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        operationRecordService.insertOperationRecord(operationRecord);
        return toAjax(institutionalManagementService.updateInstitutionalManagement(institutionalManagement));
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody InstitutionalManagement institutionalManagement)
    {
        institutionalManagement.setStatus("审核失败");
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setContent("审核失败");
        operationRecord.setManageId(institutionalManagement.getId());
        operationRecord.setTime(new Date());
        operationRecord.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        operationRecordService.insertOperationRecord(operationRecord);
        return toAjax(institutionalManagementService.updateInstitutionalManagement(institutionalManagement));
    }


    @PostMapping("/agree")
    public AjaxResult agree(@RequestBody InstitutionalManagement institutionalManagement)
    {
        institutionalManagement.setStatus("审核成功");
        institutionalManagement.setRemark("");
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setContent("审核成功");
        operationRecord.setManageId(institutionalManagement.getId());
        operationRecord.setTime(new Date());
        operationRecord.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        operationRecordService.insertOperationRecord(operationRecord);
        return toAjax(institutionalManagementService.updateInstitutionalManagement(institutionalManagement));
    }

    @GetMapping(value = "/operations/{id}")
    public AjaxResult operations(@PathVariable("id") Long id)
    {
        OperationRecord operationRecord = new OperationRecord();
        operationRecord.setManageId(id);
        List<OperationRecord> operationRecords = operationRecordService.selectOperationRecordList(operationRecord);
        return AjaxResult.success(operationRecords);
    }
}
